﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlainOldVersionControl.Domain;
using PlainOldVersionControl.ErrorHandling;
using PlainOldVersionControl.Data;
using PlainOldVersionControl.BusinessLogic.Base;
using System.IO;
using PlainOldVersionControl.BusinessLogic;
using PlainOldVersionControl.Data.Base;

namespace PlainOldVersionControl.UI
{
    public partial class MainForm : Form
    {
        string _root;

        public MainForm()
        {
            InitializeComponent();
        }

        #region Menu Events

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult re = MessageBox.Show("Are You Sure You Want To Close?", "Confirm", MessageBoxButtons.YesNo);
            if(re == System.Windows.Forms.DialogResult.Yes)
                this.Close();
        }

        private void backupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                DialogResult res = sfd.ShowDialog();

                if (res == System.Windows.Forms.DialogResult.OK || !string.IsNullOrEmpty(sfd.FileName))
                {
                    ObjectStoreDA.BackupStore(sfd.FileName);
                    MessageBox.Show("Backup Completed Successfully");
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                ErrorHandlingHelper.HandleError(ex);
            }
        }

        private void commitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new CreateCommit().ShowDialog();
            RefreshThisForm();
        }

        private void compareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvMain.SelectedRows.Count != 2)
                MessageBox.Show("You Must Select 2 Commits");

            var c1 = dgvMain.SelectedRows[0].Cells[CommitBL.ColumnNames.Tree].Value.ToString();
            var c2 = dgvMain.SelectedRows[1].Cells[CommitBL.ColumnNames.Tree].Value.ToString();

            IList<TreeBL.CompareResult> results = TreeBL.CompareTwoTrees(c1, c2);

            MessageBox.Show(TreeBL.CompareResult.SimpleToString(results));
        }

        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (!DA.DBExists())
                {
                    FolderBrowserDialog dia = new FolderBrowserDialog();
                    dia.RootFolder = Environment.SpecialFolder.Desktop;
                    dia.ShowDialog();

                    if (!string.IsNullOrEmpty(dia.SelectedPath))
                    {
                        BlobBL.InitializeNewDataBase(dia.SelectedPath);
                    }
                }
                UserSettings.Default.UserName = Environment.UserName;

                RefreshThisForm();

            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                ErrorHandlingHelper.HandleError(ex);
            }
        }

        private void RefreshThisForm()
        {
            _root = BaseBL.GetVersionRoot();
            DataTable tb = new DataTable();     
            CommitBL.GetAllCommits(tb);
            dgvMain.DataSource = tb;
            lblCurrentDirHash.Text = string.Format("Status : Current Hash={0}", TreeBL.GetDirectoryHash(_root));
        }

        private void dgvMain_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvMain.CurrentRow != null)
            {
                string hash = dgvMain.CurrentRow.Cells[CommitBL.ColumnNames.Commit].Value.ToString();
                new ViewCommit(hash).ShowDialog();
            }
        }

        

       
    }
}
